<%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2023-06-13
  Time: 17:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-3.6.3.min.js.txt"></script>
    <script>
        $(function () {
            $(":text[name=subjectname]").blur(function () {
                if (this.value == "") {
                    alert("没有填写课程名");
                    return false;
                }
                let th = $(this);
                $.ajax({
                    method: "post",
                    url: 'checkSubjectName.do',
                    data: {subjectname: th.val()},
                    async: false,
                    success: function (res) {
                        if (res == "false") {
                            th.next().html("这个名字可以用").css("color", "green");
                            return false;
                        }
                        th.next().html("这个名字已经存在！").css("color", "red");
                    },
                    error: function (e) {
                        alert("操作失败！" + JSON.stringify(e));
                    }
                })
            });
        });
    </script>

    <%--  <script>
          $(function () {
             $(":text[name=subjectname]").blur(function () {
                if(this.value==""){
                    alert("没有填写课程名");
                    return false;
                }
                let th=$(this);
                $.get("checkSubjectName.do",{subjectname:$(this).val()},function (res) {
                   if(res=="false"){
                       th.next().html("这个名字可以用").css("color","green");
                       return false;
                   }
                    th.next().html("这个名字已经存在！").css("color","red");
                },"text");
             });
          });
      </script>--%>
    <script>
        /*  function  checkName(t) {
              if(t.value==""){
                  alert("没有填名称");
                  return false;
              }
              let xhr=new XMLHttpRequest();
              xhr.onreadystatechange=function () {
                  console.log(xhr.getAllResponseHeaders());

                  if(xhr.readyState==4&&xhr.status==200){
                      let r=xhr.responseText;
                      if(r=="false"){
                          t.nextElementSibling.innerHTML="可以用";
                          return;
                      }
                      t.nextElementSibling.innerHTML="名称重复了~";
                  }
              };
              xhr.open("get","checkSubjectName.do?subjectname="+t.value,true);
              xhr.send(null);
          }*/
    </script>
</head>
<body>

<form>
    <p>
        课程名:<input type="text" required name="subjectname"/><span></span>
    </p>
    <p>
        课时:<input type="number" min="0" name="classhour"/>
    </p>
    <p>

        年级:
        <select name="gradeid">
            <c:forEach items="${grades}" var="g">
                <option value="${g.id}">${g.gname}</option>
            </c:forEach>
        </select>
    </p>
    <p>
        <input type="submit" value="保存">
    </p>
</form>
</body>
</html>
